package com.meihuak.cloud_ordering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihuak.cloud_ordering.bean.Comment;
import com.meihuak.cloud_ordering.mapper.CommentMapper;
import com.meihuak.cloud_ordering.service.CommentService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    /**
     * 通过id和类型返回对应的评论
     *
     * @param userid 被评论人id
     * @param utype  被评论人类型
     * @return 评论列表
     */
    @Override
    public List<Comment> getPostCommentById(int userid, String utype) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
//        if (utype.equals("post")){//返回帖子下的所有评论
//
//        }else if (utype.equals("business")){//返回商家下的评论
//
//        }else if (utype.equals("student")){//返回学生发出的所有评论
//
//        }
        return this.list(queryWrapper.eq("utype",utype)
                .eq("user_id",userid)
                .orderByDesc("create_date"));
    }

    /**
     * 添加评论
     *
     * @param map
     */
    @Override
    public void addCommentByPostId(Map<String, Object> map) {
        this.save(new Comment(null,(Long) map.get("postid"),
                (String) map.get("utype"),
                (int)(map.get("userid")) ,
                (String) map.get("uname"),
                (String) map.get("content"),
                0, new Date()));
    }

    /**
     * 获取评论
     *
     * @param map
     */
    @Override
    public List<Comment> getCommentByPostId(Map<String, Object> map) {
        //分页查询，点赞数优先，发布日期次之
        //返回第i页,返回m条评论
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id",(Long)map.get("postid"))
                .eq("utype",(String)map.get("utype"))
                .orderByDesc("like_count","create_date")
                .last("limit "+((int)map.get("i"))*((int)map.get("m"))
                        +","+(int)map.get("m"));

        return this.list(queryWrapper);

    }
}
